﻿Public Class SpecificationExplorer
    Inherits ExplorerTreeList
    Implements IExplorer

    Private EditPanePL As PLBase


    Dim section As String
    Dim description As String
    Dim revisionIsActivated As Boolean
    Dim testIsEnabled As Boolean

#Region "Initialization"

    Public Sub New(ByVal aQueryName As String)
        MyBase.New(aQueryName)
        'AddHandler InitTreeActionOptionList, AddressOf Init_TreeActionOptionList
        'AddHandler MainContextMenuBeforePopup, AddressOf Spec_ContextMenuBeforePopup
    End Sub

    Public Sub New()
        MyBase.New()
        'AddHandler InitTreeActionOptionList, AddressOf Init_TreeActionOptionList
        'AddHandler MainContextMenuBeforePopup, AddressOf Spec_ContextMenuBeforePopup
    End Sub

    Public Overrides Sub ActionOptionLoad()
        MyBase.ActionOptionLoad()
        MetaActionOptionListLoad()
    End Sub

    Public Overridable Sub MetaActionOptionListLoad() Implements IExplorer.MetaActionOptionListLoad
        MetaControlTreeListActionOptionList.Add(New ActionOption("SpecRoot", "ACT_00195", "Grid Update", AddressOf Specification_GridViewUpdate, True, True, "SpecificationList"))
        MetaControlTreeListActionOptionList.Add(New ActionOption("SpecRoot", "ACT_00196", "Card View", AddressOf Specification_LayoutViewUpdate, True, True, "SpecificationList"))

        MetaControlTreeListActionOptionList.Add(New ActionOption("SpecOption", "ACT_00074", "Create new revision", AddressOf CreateNewSpecRevision, True, True, "Specification"))
        MetaControlTreeListActionOptionList.Add(New ActionOption("SpecOption", "ACT_00080", "Delete Spec", AddressOf RemoveSpec, True, True, "Specification"))

        MetaControlTreeListActionOptionList.Add(New ActionOption("RevisionOption", "ACT_00082", "Delete Revision", AddressOf RemoveSpecRevision, True, True, "SpecificationRevision"))
        MetaControlTreeListActionOptionList.Add(New ActionOption("RevisionOption", "ACT_00134", "Activate", AddressOf ActivateSpecRevision, True, True, "SpecificationRevision"))
        MetaControlTreeListActionOptionList.Add(New ActionOption("RevisionOption", "ACT_00217", "Un Activate", AddressOf UnActivateSpecRevision, True, True, "SpecificationRevision"))



        MetaControlTreeListActionOptionList.Add(New ActionOption("RevisionOption", "ACT_00079", "Get  Active Test", AddressOf GetSpecificationTestRevision, True, True, "SpecificationRevision"))
        MetaControlTreeListActionOptionList.Add(New ActionOption("RevisionCopy", "ACT_00215", "Copy to another Spec", AddressOf ActionMenu_CopyToAnotherSpec, True, True, "SpecificationRevision"))
        MetaControlTreeListActionOptionList.Add(New ActionOption("RevisionCopy", "ACT_00133", "Copy to new revision", AddressOf CopySpecRevisionToNewRevision, True, True, "SpecificationRevision"))
    End Sub

#End Region

#Region "Process"

    Public Overrides Sub PopulateDetailSection() Implements IExplorer.PopulateDetailSection
        Me.Cursor = Cursors.WaitCursor
        '
        If aGridControl IsNot Nothing Then
            aGridControl.Dispose()
            aGridControl = Nothing
        End If
        '
        Dim guidEnfant As Guid
        Dim guidParent As Guid
        Dim aPLName As String


        Dim aPaneMetaQueryName As String
        Dim Ametaquerie As MetaQuery
        Dim frmCaption As String = "Specification List"


        Try
            guidEnfant = DirectCast(NodeDataRow(Me.KeyFieldName), Guid)
            aPLName = DirectCast(NodeDataRow("PL"), String)
            SpecExplorer = New SpecExplorerNodeInfo(NodeDataRow)

            aPaneMetaQueryName = DirectCast(NodeDataRow("MetaQuerie"), String)
            Ametaquerie = MetaQuery.GetMetaQueryByName(aPaneMetaQueryName)

            'c<est ici qu<on bind les info du noeud qui articule le Edit PANE
            EditPanePL = PLBase.CreateInstanceOf(aPLName, aPaneMetaQueryName)
            aGridControl = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = EditPanePL, .SecureGrid = True, .Name = Ametaquerie.Caption, .FormCtrlCD = MyBase.PanelName}

            AddHandler aGridControl.AfterSave, AddressOf AfterSave
            '
            'If FocusedNodeLevel > SpecTreeListLevel.Root Then
            '    guidParent = DirectCast(NodeDataRow(Me.ParentFieldName), Guid)
            'End If

            '
            'section = NodeDataRow("Section").ToString

            'B.Desjardins 2014-09-27
            'todo: add a override for Sysadmin Group or power user
            revisionIsActivated = CBool(NodeDataRow("RevisionActivated").ToString)
            testIsEnabled = CBool(NodeDataRow("TestEnabled").ToString)
            '
            Select Case aPaneMetaQueryName
                Case "SpecificationList"
                    frmCaption = "Specification List"
                    ShowAllSpecification()

                Case "Specification"
                    frmCaption = BreadCrumbText
                    ShowBySpecification()

                Case "SpecificationRevision"
                    frmCaption = BreadCrumbText
                    ShowSpecRevision()

                Case "SpecificationValue"
                    frmCaption = BreadCrumbText
                    ShowSpecValue()

                Case "SpecificationAttribute"
                    frmCaption = BreadCrumbText
                    ShowSpecAttribute()

                Case "SpecificationTestRevision"
                    frmCaption = BreadCrumbText
                    ShowSpecTest()

            End Select
        Catch ex As Exception
            MessageWindow.ShowMessage(ex)
        End Try


        'On trouve la form pour effacer les controle dedans et ensuite remettre le bon.
        'Comme ca on change le Detail selon la ligne dans L'explorer
        For Each aFrm As XtraForm In Me.ParentForm.MdiChildren
            If aFrm.Name = PanelName Then
                aFrm.Controls.Clear()
                aFrm.Controls.Add(aGridControl)
                aFrm.Text = frmCaption
                aFrm.Show()
                aFrm.Focus()
            End If
            aFrm.Refresh()
        Next

        Me.Cursor = Cursors.Default
    End Sub

#End Region



#Region "Sub routine"


    Private Sub AfterSave()
        ReloadTree(ReloadContext.AfterUpdate)
        Me.ParentForm.ActiveMdiChild.Text = BreadCrumbText
    End Sub

    Public Overridable Sub ShowAllSpecification()
        ' Nothing to do
        ' the extendedtreelist action the MQ listed in explorer

    End Sub

    Public Overridable Sub ShowBySpecification()
        EditPanePL.SQLWhere = String.Format("SpecID = '{0}'", SpecExplorer.KeyID)
    End Sub

    Public Overridable Sub ShowSpecRevision()
        Dim actionItem As ActionOption
        EditPanePL.SQLWhere = String.Format("SpecificationRevId = '{0}'", SpecExplorer.KeyID)

        AddHandler aGridControl.InitNewMasterRow, AddressOf InitNewMasterRowSpecRevision

        AddHandler aGridControl.InitNewDetailRow, AddressOf InitNewDetailRowSpecRevision
        AddHandler aGridControl.DetailRowClick, AddressOf SpecificationRevisionDetailRowClick
        AddHandler aGridControl.ProcessCurrentDetailRecord, AddressOf toto
        AddHandler aGridControl.AfterViewRegistered, AddressOf AfterViewRegistered
        AddHandler aGridControl.DetailContextMenuBeforePopup, AddressOf SpecTestContextMenuBeforePopup 'manage Grid Menu for detail Tab

        aGridControl.GridActionOptionList.Add(New ActionOption("RevisionOption", "ACT_00133", "Copy to new revision", AddressOf CopySpecRevisionToNewRevision, True, True))
        aGridControl.GridActionOptionList.Add(New ActionOption("RevisionCopy", "ACT_00215", "Copy to another Spec", AddressOf ActionMenu_CopyToAnotherSpec, True, True, "SpecificationRevision"))


        If Not SpecExplorer.RevisionActivated Then
            aGridControl.GridActionOptionList.Add(New ActionOption("RevisionOption", "ACT_00134", "Activate", AddressOf ActivateSpecRevision, True, True))
            aGridControl.GridActionOptionList.Add(New ActionOption("RevisionOption", "ACT_00079", "Get  Active Test", AddressOf GetSpecificationTestRevision, True, True, "SpecificationRevision"))

            'Menu will show on detail if "SpecificationTestRevision" has focus , Handled by aGridControl.DetailContextMenuBeforePopup
            actionItem = New ActionOption("SpecTest", "ACT_00219", "CARL", True, True, "SpecificationTestRevision")
            aGridControl.GridActionOptionList.Add(actionItem)
            aGridControl.GridActionOptionList.Add(New ActionOption("SpecTest", "ACT_00136", "Revise", AddressOf ReviseSpecTest, False, True, actionItem, "SpecificationTestRevision"))
            aGridControl.GridActionOptionList.Add(New ActionOption("SpecTest", "ACT_00137", "Limit", AddressOf LimitSpecTest, False, True, actionItem, "SpecificationTestRevision"))



            aGridControl.AllowCreate = True
            aGridControl.AllowDelete = True
            aGridControl.AllowUpdate = True
        Else
            aGridControl.GridActionOptionList.Add(New ActionOption("RevisionOption", "ACT_00134", "Activate", AddressOf ActivateSpecRevision, False, True))

            aGridControl.AllowCreate = False
            aGridControl.AllowDelete = False
            aGridControl.AllowUpdate = False
        End If


        If Not SpecExplorer.RevisionActivated AndAlso Not SpecExplorer.TestEnabled Then
            aGridControl.GridActionOptionList.Add(New ActionOption("RevisionEnableOption", "ACT_00135", "Enable Test editing", AddressOf EnableSpecTestEditing, True, True))
        Else
            aGridControl.GridActionOptionList.Add(New ActionOption("RevisionEnableOption", "ACT_00135", "Enable Test editing", AddressOf EnableSpecTestEditing, False, True))
        End If


        If Not SpecExplorer.RevisionActivated AndAlso Not SpecExplorer.AttributeEnaled Then
            aGridControl.GridActionOptionList.Add(New ActionOption("RevisionEnableOption", "ACT_00135", "Enable Attribute editing", AddressOf EnableSpecAttributeEditing, True, True))
        Else
            aGridControl.GridActionOptionList.Add(New ActionOption("RevisionEnableOption", "ACT_00135", "Enable Attribute editing", AddressOf EnableSpecAttributeEditing, False, True))
        End If


    End Sub

    Public Overridable Sub ShowSpecValue()
        EditPanePL.SQLWhere = String.Format("SpecID_FK = '{0}'", SpecExplorer.ParentID)
        AddHandler aGridControl.InitNewMasterRow, AddressOf InitNewMasterRowSpecValue
    End Sub

    Public Overridable Sub ShowSpecAttribute()
        aPLSpecAttribute = PLBase.CreateInstanceOf(PLTypeOf.SpecificationAttribute_PL, "SpecificationAttribute")
        aPLSpecAttribute.SQLWhere = String.Format("SpecificationRevId_FK = '{0}'", SpecExplorer.ParentID)
        aPLSpecAttribute.SpecificationRevId_FK = SpecExplorer.ParentID
        aGridControl.Name = "SpecificationAttribute"
        aGridControl.Pl = aPLSpecAttribute

        AddHandler aGridControl.InitNewMasterRow, AddressOf InitNewRowSpecAttribute
        AddHandler aGridControl.AfterLoad, AddressOf DetailGridAfterLoad

        If revisionIsActivated Then
            aGridControl.AllowCreate = False
            aGridControl.AllowDelete = False
            aGridControl.AllowUpdate = False
        Else
            aGridControl.AllowCreate = True
            aGridControl.AllowDelete = True
            aGridControl.AllowUpdate = True
        End If
    End Sub

    Public Overridable Sub ShowSpecTest()
        Dim actionItem As ActionOption

        aPLSpecTestRev = PLBase.CreateInstanceOf(PLTypeOf.SpecificationTestRevision_PL, "SpecificationTestRevision")
        aPLSpecTestRev.SQLWhere = String.Format("SpecificationRevId_FK = '{0}'", SpecExplorer.ParentID)
        aPLSpecTestRev.SpecificationRevId_FK = SpecExplorer.ParentID
        aGridControl.Name = "SpecificationTestRevision"
        aGridControl.Pl = aPLSpecTestRev

        AddHandler aGridControl.ValidateMasterRow, AddressOf SpecTestRevisionValidate
        AddHandler aGridControl.MainContextMenuBeforePopup, AddressOf SpecTestContextMenuBeforePopup
        AddHandler aGridControl.MasterRowClick, AddressOf SpecificationTestRowClick
        AddHandler aGridControl.InitNewMasterRow, AddressOf InitNewRowSpecTestRevision


        If Not SpecExplorer.NodeActivated AndAlso SpecExplorer.TestEnabled Then
            actionItem = New ActionOption("SpecTest", "ACT_00219", "CARL", True, True, "SpecificationTestRevision")
            aGridControl.GridActionOptionList.Add(actionItem)
            aGridControl.GridActionOptionList.Add(New ActionOption("SpecTest", "ACT_00136", "Revise", AddressOf ReviseSpecTest, False, True, actionItem, "SpecificationTestRevision"))
            aGridControl.GridActionOptionList.Add(New ActionOption("SpecTest", "ACT_00137", "Limit", AddressOf LimitSpecTest, False, True, actionItem, "SpecificationTestRevision"))


            aGridControl.AllowCreate = True
            aGridControl.AllowDelete = True
            aGridControl.AllowUpdate = True
        Else
            aGridControl.GridActionOptionList.Add(New ActionOption("SpecTest", "ACT_00136", "Revise", AddressOf ReviseSpecTest, False, True))
            aGridControl.GridActionOptionList.Add(New ActionOption("SpecTest", "ACT_00137", "Limit", AddressOf LimitSpecTest, False, True))

            aGridControl.AllowCreate = False
            aGridControl.AllowDelete = False
            aGridControl.AllowUpdate = False
        End If

    End Sub

    Private Sub Specification_LayoutViewUpdate()
        Try

            Me.Cursor = Cursors.WaitCursor

            PL = PLBase.CreateInstanceOf(PLTypeOf.Specification_PL, "Specification")
            aGridLayout = New GridLayoutPL() With {.Dock = DockStyle.Fill, .Pl = PL, .FormCtrlCD = Nothing}

            For Each aFrm As XtraForm In Me.ParentForm.MdiChildren
                If aFrm.Name = PanelName Then
                    aFrm.Controls.Clear()
                    aFrm.Controls.Add(aGridLayout)
                    aFrm.Text = aGridLayout.Pl.QueryName
                    aFrm.Show()
                End If
                aFrm.Refresh()
            Next

            AddHandler aGridLayout.InitNewMasterRow, AddressOf InitNewLayoutRow

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub

    Private Sub Specification_GridViewUpdate()
        Me.Cursor = Cursors.WaitCursor


        PL = PLBase.CreateInstanceOf(PLTypeOf.Specification_PL, "Specification")
        aGridControl = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = PL, .CtrlName = "FRM_00017"}
        AddHandler aGridControl.InitNewMasterRow, AddressOf InitNewMasterRow
        For Each aFrm As XtraForm In Me.ParentForm.MdiChildren
            If aFrm.Name = PanelName Then
                aFrm.Controls.Clear()
                aFrm.Controls.Add(aGridControl)
                aFrm.Text = aGridControl.Pl.QueryName
                aFrm.Show()
            End If
            aFrm.Refresh()
        Next

        Me.Cursor = Cursors.Default

    End Sub


#End Region

    Private Sub toto(sender As Object, e As ValidateRowEventArgs, index As Integer)

    End Sub




End Class